package com.ds.lens.data.service;

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.ds.lens.data.common.asserts.Asserts;
import com.ds.lens.data.common.constant.Constants;
import com.ds.lens.data.service.dao.LensIntegrationDao;
import com.ds.lens.data.service.dao.entity.LensIntegration;
import com.ds.lens.data.vo.in.PagedConditionInVO;
import com.ds.lens.data.vo.in.v2.integration.LensIntegrationListInVO;
import com.ds.lens.data.vo.out.PageResult;
import com.ds.lens.data.vo.out.v2.integration.LensIntegrationDetailOutVO;
import com.ds.lens.data.vo.out.v2.integration.LensIntegrationOutVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-11-04
 */
@Slf4j
@Service
public class LensIntegrationService extends BaseService {

    @Autowired
    LensIntegrationDao lensIntegrationDao;

    /**
     * Query integration menu
     *
     * @return LensIntegrationDetailOutVO
     */
    public List<LensIntegrationOutVO> findLensIntegrationMenu() {
        List<LensIntegration> lensIntegrations = lensIntegrationDao.queryLensIntegrationMenu();
        if (CollectionUtils.isNotEmpty(lensIntegrations)) {
            return lensIntegrations.stream().map(lensIntegration -> {
                LensIntegrationOutVO lensIntegrationOutVO = new LensIntegrationDetailOutVO();
                BeanUtils.copyProperties(lensIntegration, lensIntegrationOutVO);
                return lensIntegrationOutVO;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    /**
     * Query integration information
     *
     * @param id primary key
     * @return LensIntegrationDetailOutVO
     */
    public LensIntegrationDetailOutVO findLensIntegrationsById(long id) {
        LensIntegration lensIntegration = lensIntegrationDao.selectByPrimaryKey(id);
        Asserts.assertNotNull(lensIntegration, Constants.ErrorCode.PARAM_ERROR, "Lens integration does not exist.");
        LensIntegrationDetailOutVO vo = new LensIntegrationDetailOutVO();
        BeanUtils.copyProperties(lensIntegration, vo);
        return vo;
    }

    /**
     * Paging for query integrations
     *
     * @param conditionInVO PagedConditionInVO<LensIntegrationListInVO>
     * @return PageResult<LensIntegrationDetailOutVO>
     */
    public PageResult<LensIntegrationOutVO> findLensIntegrationsByPage(PagedConditionInVO<LensIntegrationListInVO> conditionInVO) {
        getPageCondition(conditionInVO);
        Page<LensIntegration> page = lensIntegrationDao.queryLensIntegration();
        if (CollectionUtils.isEmpty(page.getResult())) {
            return PageResult.of(Collections.emptyList(), 0L, page.getPageNum(), page.getPageSize());
        }

        return PageResult.of(getLensIntegrationsVOS(page), page.getTotal(), page.getPageNum(), page.getPageSize());
    }

    private List<LensIntegrationOutVO> getLensIntegrationsVOS(Page<LensIntegration> page) {
        List<LensIntegrationOutVO> result = Lists.newArrayListWithCapacity(page.getResult().size());
        page.getResult().forEach(lensIntegration -> {
            LensIntegrationOutVO lensIntegrationsVO = new LensIntegrationOutVO();
            BeanUtils.copyProperties(lensIntegration, lensIntegrationsVO);
            result.add(lensIntegrationsVO);
        });
        return result;
    }
}
